import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.StrUtil;
import entity.TDYFileInfo;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;

import java.io.StringWriter;
import java.util.Date;
import java.util.Properties;

/**
 * @Description:
 * @author: knight
 * @Date: 2018/9/9 22:00
 */
public class Main {

    public static void main(String[] args) {
        String modelName = "DeviceMaintainRecord";
        String annotation = "农机养护记录";
        String pageName = "device_maintain_record";
        generatedSaveMethod(modelName, annotation, pageName);
    }


    /**
     * 生成正常的代码
     *
     * @param modelName  方法名
     * @param annotation 注释
     * @param pageName   页面名称
     */
    private static void generatedNormal(String modelName, String annotation, String pageName) {
        generatedServiceModel(modelName, annotation, "");
        generatedAPIModel(modelName, annotation, "");
        generatedController(modelName, annotation, pageName, "");
    }

    /**
     * 生成保存方法
     */
    private static void generatedSaveMethod(String modelName, String annotation, String pageName) {
        String saveMethod = "1";
        generatedServiceModel(modelName, annotation, saveMethod);
        generatedAPIModel(modelName, annotation, saveMethod);
        generatedController(modelName, annotation, pageName, saveMethod);
    }


    /**
     * 生成Controller
     *
     * @param modelName  model名称
     * @param annotation 注释
     */
    private static void generatedController(String modelName, String annotation, String pageName, String saveMethod) {
        String controllerPath = TDYFileInfo.WEB_CONTROLLER_PATH + "doc\\";
        String controllerName = modelName + "Controller.java";
        TDYFileInfo controllerTdyFileInfo = new TDYFileInfo(controllerPath, controllerName, modelName);
        controllerTdyFileInfo.setAnnotation(annotation);
        controllerTdyFileInfo.setPageName(pageName);
        controllerTdyFileInfo.setSaveMethod(saveMethod);
        String controllerTemplateName = "controller";
        docGenerated(controllerTdyFileInfo, controllerTemplateName);


        String pageListPath = TDYFileInfo.WEB_PAGE_PATH + "doc\\";
        String pageListName = pageName + "_list.ftl";
        TDYFileInfo pageListTdyFileInfo = new TDYFileInfo(pageListPath, pageListName, modelName);
        pageListTdyFileInfo.setAnnotation(annotation);
        pageListTdyFileInfo.setPageName(pageName);
        pageListTdyFileInfo.setSaveMethod(saveMethod);
        String pageListTemplateName = "pageList";
        docGenerated(pageListTdyFileInfo, pageListTemplateName);


        String detailPath = TDYFileInfo.WEB_PAGE_PATH + "doc\\";
        String detailName = pageName + "_detail.ftl";
        TDYFileInfo detailTdyFileInfo = new TDYFileInfo(detailPath, detailName, modelName);
        detailTdyFileInfo.setAnnotation(annotation);
        detailTdyFileInfo.setPageName(pageName);
        detailTdyFileInfo.setSaveMethod(saveMethod);
        String detailTemplateName = "pageDetail";
        docGenerated(detailTdyFileInfo, detailTemplateName);


        if (StrUtil.isNotBlank(saveMethod)) {

            String addPath = TDYFileInfo.WEB_PAGE_PATH + "doc\\";
            String addName = pageName + "_add.ftl";
            TDYFileInfo addTdyFileInfo = new TDYFileInfo(addPath, addName, modelName);
            addTdyFileInfo.setAnnotation(annotation);
            addTdyFileInfo.setPageName(pageName);
            addTdyFileInfo.setSaveMethod(saveMethod);
            String addTemplateName = "pageAdd";
            docGenerated(addTdyFileInfo, addTemplateName);
        }


        String docJSPath = TDYFileInfo.WEB_JS_PATH + "doc\\";
        String docJSName = pageName + ".js";
        TDYFileInfo docJSTdyFileInfo = new TDYFileInfo(docJSPath, docJSName, modelName);
        docJSTdyFileInfo.setAnnotation(annotation);
        docJSTdyFileInfo.setPageName(pageName);
        docJSTdyFileInfo.setSaveMethod(saveMethod);
        String docJSTemplateName = "js";
        docGenerated(docJSTdyFileInfo, docJSTemplateName);
    }


    /**
     * 生成API项目相关类
     *
     * @param modelName model名称
     */
    private static void generatedAPIModel(String modelName, String annotation, String saveMethod) {
        String apiClassPath = TDYFileInfo.API_PATH + "api\\";
        String apiClassName = modelName + "Api.java";
        TDYFileInfo apiTdyFileInfo = new TDYFileInfo(apiClassPath, apiClassName, modelName);
        apiTdyFileInfo.setAnnotation(annotation);
        apiTdyFileInfo.setSaveMethod(saveMethod);
        String apiTemplateName = "api";
        docGenerated(apiTdyFileInfo, apiTemplateName);


        String domainClassPath = TDYFileInfo.API_PATH + "domain\\";
        String DTOClassName = modelName + "DTO.java";
        TDYFileInfo DTOTdyFileInfo = new TDYFileInfo(domainClassPath, DTOClassName, modelName);
        DTOTdyFileInfo.setAnnotation(annotation);
        DTOTdyFileInfo.setSaveMethod(saveMethod);
        String DTOTemplateName = "DTO";
        docGenerated(DTOTdyFileInfo, DTOTemplateName);


        String queryClassName = modelName + "Query.java";
        TDYFileInfo queryTdyFileInfo = new TDYFileInfo(domainClassPath, queryClassName, modelName);
        queryTdyFileInfo.setAnnotation(annotation);
        queryTdyFileInfo.setSaveMethod(saveMethod);
        String queryTemplateName = "query";
        docGenerated(queryTdyFileInfo, queryTemplateName);


    }


    /**
     * 生成service项目相关类
     *
     * @param modelName model名称
     */
    private static void generatedServiceModel(String modelName, String annotation, String saveMethod) {
        generatedApiImpl(modelName, annotation, saveMethod);
        generatedService(modelName, annotation, saveMethod);
        generatedDao(modelName, annotation);
        generatedMapper(modelName, annotation);
    }


    /**
     * 生成Mapper
     *
     * @param modelName model名称
     */
    private static void generatedMapper(String modelName, String annotation) {
        String mapperClassPath = TDYFileInfo.SERVICE_PATH + "mapper\\";
        String mapperClassName = modelName + "Mapper.java";
        String mapperTemplateName = "mapper";
        TDYFileInfo serviceTdyFileInfo = new TDYFileInfo(mapperClassPath, mapperClassName, modelName);
        serviceTdyFileInfo.setAnnotation(annotation);
        docGenerated(serviceTdyFileInfo, mapperTemplateName);
    }


    /**
     * 生成dao
     *
     * @param modelName model名称
     */
    private static void generatedDao(String modelName, String annotation) {
        String daoClassPath = TDYFileInfo.SERVICE_PATH + "dao\\";
        String daoClassName = modelName + "Dao.java";
        String daoTemplateName = "dao";
        TDYFileInfo serviceTdyFileInfo = new TDYFileInfo(daoClassPath, daoClassName, modelName);
        serviceTdyFileInfo.setAnnotation(annotation);
        docGenerated(serviceTdyFileInfo, daoTemplateName);


        String daoImplClassPath = daoClassPath + "impl\\";
        String daoImplClassName = modelName + "DaoImpl.java";
        TDYFileInfo serviceImplTdyFileInfo = new TDYFileInfo(daoImplClassPath, daoImplClassName, modelName);
        serviceImplTdyFileInfo.setAnnotation(annotation);
        String serviceImplTemplateName = "daoImpl";
        docGenerated(serviceImplTdyFileInfo, serviceImplTemplateName);
    }


    /**
     * 生成Service
     *
     * @param modelName
     */
    private static void generatedService(String modelName, String annotation, String saveMethod) {
        String serviceClassPath = TDYFileInfo.SERVICE_PATH + "service\\";
        String serviceClassName = modelName + "Service.java";
        String serviceTemplateName = "service";
        TDYFileInfo serviceTdyFileInfo = new TDYFileInfo(serviceClassPath, serviceClassName, modelName);
        serviceTdyFileInfo.setAnnotation(annotation);
        serviceTdyFileInfo.setSaveMethod(saveMethod);
        docGenerated(serviceTdyFileInfo, serviceTemplateName);


        String serviceImplClassPath = serviceClassPath + "impl\\";
        String serviceImplClassName = modelName + "ServiceImpl.java";
        TDYFileInfo serviceImplTdyFileInfo = new TDYFileInfo(serviceImplClassPath, serviceImplClassName, modelName);
        serviceImplTdyFileInfo.setAnnotation(annotation);
        serviceImplTdyFileInfo.setSaveMethod(saveMethod);
        String serviceImplTemplateName = "serviceImpl";
        docGenerated(serviceImplTdyFileInfo, serviceImplTemplateName);
    }

    /**
     * 生成ApiImpl
     *
     * @param modelName
     */
    public static void generatedApiImpl(String modelName, String annotation, String saveMethod) {
        String templateName = "ApiImpl";
        String apiImplPath = TDYFileInfo.SERVICE_PATH + "api\\impl\\";
        String fileName = modelName + "ApiImpl.java";
        TDYFileInfo apiImplTdyFileInfo = new TDYFileInfo(apiImplPath, fileName, modelName);
        apiImplTdyFileInfo.setAnnotation(annotation);
        apiImplTdyFileInfo.setSaveMethod(saveMethod);
        docGenerated(apiImplTdyFileInfo, templateName);
    }


    private static void docGenerated(TDYFileInfo fileInfo, String templateName) {
        Properties prop = new Properties();
        //设置velocity的编码
        prop.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8");
        prop.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
        prop.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
        prop.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, ClassUtil.getClassPath());
        Velocity.init(prop);
        Template template = Velocity.getTemplate("doc/" + templateName + ".vtl", "UTF-8");
        VelocityContext context = new VelocityContext();

        context.put("modelName", fileInfo.getModelName());
        context.put("lowerModelName", StrUtil.lowerFirst(fileInfo.getModelName()));
        context.put("annotation", fileInfo.getAnnotation());
        context.put("author", fileInfo.getAuthor());
        context.put("createTime", DateUtil.formatDateTime(new Date()));
        context.put("pageName", fileInfo.getPageName());
        context.put("saveMethod", fileInfo.getSaveMethod());
        StringWriter stringWriter = new StringWriter();
        template.merge(context, stringWriter);
        String filePath = fileInfo.getFilePath() + fileInfo.getFileName();
        //  FileUtil.newFile(filePath).deleteOnExit();
        FileUtil.writeUtf8String(stringWriter.toString(), filePath);
    }

    public void genFileInfo() {
        String modelName = "HarvestRecord";
        String parentPath = "D:\\gitProject\\nht\\t2\\tdy-nht\\tdy-nht-soa-parent\\";
        String servicePath = parentPath + "tdy-nht-service\\src\\main\\java\\com\\tdy\\nht\\doc\\";


        String apiImplPath = servicePath + "api\\impl\\";
        String apiImplName = modelName + "ApiImpl.java";
        FileUtil.newFile(apiImplPath + apiImplName).deleteOnExit();

        String serviceClassPath = servicePath + "service\\";
        String serviceImplClassPath = serviceClassPath + "impl\\";
        String serviceClassName = modelName + "Service.java";
        String serviceImplClassName = modelName + "ServiceImpl.java";
        FileUtil.newFile(serviceClassPath + serviceClassName).deleteOnExit();
        FileUtil.newFile(serviceImplClassPath + serviceImplClassName).deleteOnExit();


        String apiPath = parentPath + "tdy-nht-api\\src\\main\\java\\com\\tdy\\nht\\doc\\";

        String apiServicePath = apiPath + "api\\";
        String apiServiceName = modelName + "Api.java";
        FileUtil.newFile(apiServicePath + apiServiceName).deleteOnExit();

        String domainPath = apiPath + "domain\\";
        String domainDTOName = modelName + "DTO.java";
        String domainQueryName = modelName + "Query.java";

        FileUtil.newFile(domainPath + domainDTOName).deleteOnExit();
        FileUtil.newFile(domainPath + domainQueryName).deleteOnExit();
    }
}
